package com.yanqu.road.server.cmd.activity.apprentice;

import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandPatronsData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ApprenticeProto;
import com.yanqu.road.pb.activity.HsIslandProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeActivity;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeMgr;
import com.yanqu.road.server.manager.activity.apprentice.pb.ApprenticePb;
import com.yanqu.road.server.manager.activity.hsisland.*;
import com.yanqu.road.server.manager.activity.hsisland.pb.CrossHsIslandActivityPb;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = Protocol.C2_APPRENTICE_ENTER_ACTIVITY, desc = "爱徒养成-进入活动")
public class CrossApprenticeEnterActivityCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ApprenticeProto.ApprenticeEnterActivityReqMsg reqMsg = ApprenticeProto.ApprenticeEnterActivityReqMsg.parseFrom(packet.getBytes());
        ApprenticeProto.ApprenticeEnterActivityRespMsg.Builder respMsg = ApprenticeProto.ApprenticeEnterActivityRespMsg.newBuilder();
        CrossApprenticeActivity activity = CrossApprenticeMgr.getActivity(reqMsg.getActivityId());
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_APPRENTICE_ENTER_ACTIVITY, respMsg));
            return;
        }
        //存储userBase
        if(reqMsg.hasUserInfo()){
            UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserInfo());
            CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
        }
        //进入活动
        try {
            String initReward = activity.enterActivity(packet.getPlayerId());
            ApprenticeUserData userData = activity.getUserData(packet.getPlayerId());
            respMsg.setRet(0);
            respMsg.setUserData(ApprenticePb.buildUserData(userData, activity.getCanSelectProjectNum(userData)));
            respMsg.setInitReward(initReward);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_APPRENTICE_ENTER_ACTIVITY_FROM_CROSS, respMsg));
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_APPRENTICE_ENTER_ACTIVITY, respMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
